import { REQUEST_FAILURE } from "./const";
import logger from "./log";

const handleErrorsUncaught = (err, req, res, next) => {
    if (err) {
        if (err instanceof SyntaxError) {
            res.status(400)
                .json({ code: REQUEST_FAILURE, message: err.message, body: err.body });
        } else {
            logger.error("Uncaught error: ", err);
            res.status(500)
                .json({ code: REQUEST_FAILURE, message: "System errors" });
        }
    }
    next();
};

export default handleErrorsUncaught;
